<template>
	<xn-form-container
		:title="formData.id ? '编辑' : '新增'"
		:width="1000"
		:visible="visible"
		:destroy-on-close="true"
		@close="onClose"
	>
		<a-form
			ref="formRef"
			:model="formData"
			autocomplete="off"
			labelAlign="right"
			:rules="formRules"
			:wrapper-col="{ span: 16 }"
			:label-col="{ span: 8 }"
		>
			<a-row>
				<a-col :span="12">
					<a-form-item label="客户名称:" name="custName">
						<a-input v-model:value="formData.custName" placeholder="请输入客户名称" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="客户信用代码:" name="custCode">
						<a-input
							style="width: 100%"
							v-model:value="formData.custCode"
							placeholder="请输入客户信用代码"
							allow-clear
						/>
					</a-form-item>
				</a-col>
			</a-row>
			<a-row>
				<a-col :span="12">
					<a-form-item label="省市区:" name="address">
						<a-cascader
							style="width: 100%"
							v-model:value="formData.address"
							:options="areaList"
							@change="onCascaderChange"
							placeholder="请选择省市区"
						/>
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="详细地址:" name="detailAddr">
						<a-input v-model:value="formData.detailAddr" placeholder="请输入详细地址" allow-clear />
					</a-form-item>
				</a-col>
			</a-row>

			<a-row>
				<a-col :span="12">
					<a-form-item label="上级客户:" name="superCust">
						<xn-page-selector
							:field-names="{ label: 'custName', value: 'id' }"
							:page-function="(param) => customerApi.getPage(param)"
							:show-search="true"
							search-field="custName"
							ref="parentCustomerRef"
							v-model:value="formData.superCust"
							placeholder="请选择上级客户"
						>
						</xn-page-selector>
					</a-form-item>
				</a-col>
			</a-row>
			<a-row>
				<a-col :span="12">
					<a-form-item label="行业:" name="industry">
						<a-select
							placeholder="请选择行业"
							:options="
								tool.dictTypeList('客户')[1].children.map((item) => {
									return {
										value: item['dictValue'],
										label: item['name']
									}
								})
							"
							v-model:value="formData.industry"
							style="width: 100%"
						>
						</a-select>
					</a-form-item>
				</a-col>
			</a-row>
			<a-row>
				<a-col :span="12">
					<a-form-item label="公司电话:" name="companyPhone">
						<a-input v-model:value="formData.companyPhone" placeholder="请输入公司电话" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="联系人:" name="contacts">
						<a-input v-model:value="formData.contacts" placeholder="请输入联系人" allow-clear />
					</a-form-item>
				</a-col>
			</a-row>
			<a-row>
				<a-col :span="12">
					<a-form-item label="联系人电话:" name="contactsPhone">
						<a-input v-model:value="formData.contactsPhone" placeholder="请输入联系人电话" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="客户背景:" name="custBackground">
						<a-select
							:getPopupContainer="(trigger) => trigger.parentNode"
							v-model:value="formData.custBackground"
							placeholder="请选择客户背景"
						>
							<a-select-option key="1" value="政府机关"></a-select-option>
							<a-select-option key="2" value="国有企业"></a-select-option>
							<a-select-option key="3" value="私营企业"></a-select-option>
							<a-select-option key="4" value="外资企业"></a-select-option>
							<a-select-option key="5" value="世界500强"></a-select-option>
							<a-select-option key="0" value="其它"></a-select-option>
						</a-select>
					</a-form-item>
				</a-col>
			</a-row>
			<a-row>
				<a-col :span="12">
					<a-form-item label="负责人:" name="headId">
						<xn-page-selector
							@change="onHeadChange"
							:field-names="{ label: 'name', value: 'id' }"
							:page-function="(param) => bizUserApi.userPage(param)"
							:show-search="true"
							search-field="name"
							ref="userRef"
							v-model:value="formData.headId"
							placeholder="请选择负责人"
						>
						</xn-page-selector>
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="备注:" name="remark">
						<a-textarea
							:autoSize="{ minRows: 4, maxRows: 4 }"
							v-model:value="formData.remark"
							placeholder="请输入备注"
						/>
					</a-form-item>
				</a-col>
			</a-row>
			<a-row>
				<a-col :span="4">
					<div style="text-align: right">
						<span>开票信息:</span>
					</div>
				</a-col>
			</a-row>
			<a-divider />
			<a-row>
				<a-col :span="12">
					<a-form-item label="发票抬头:" name="invoiceHeader">
						<a-input v-model:value="formData.invoiceHeader" placeholder="请输入发票抬头" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="税号:" name="dutyPara">
						<a-input v-model:value="formData.dutyPara" placeholder="请输入税号" allow-clear />
					</a-form-item>
				</a-col>
			</a-row>

			<a-row>
				<a-col :span="12">
					<a-form-item label="开户行:" name="bank">
						<a-input v-model:value="formData.bank" placeholder="请输入开户行" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="银行账户:" name="bankAccount">
						<a-input v-model:value="formData.bankAccount" placeholder="请输入银行账户" allow-clear />
					</a-form-item>
				</a-col>
			</a-row>
		</a-form>
		<template #footer>
			<a-button type="primary" @click="onSubmit" :loading="submitLoading">提交</a-button>
		</template>
	</xn-form-container>
</template>

<script setup name="productForm">
	import { required, rules } from '@/utils/formRules'
	import customerApi from '@/api/base_data/customerApi'
	import { getCombinationList } from '@/utils/areaUtil'
	import bizUserApi from '@/api/biz/bizUserApi'
	import xnPageSelector from '@/components/XnPageSelect/index.vue'
	import tool from '@/utils/tool'
	const areaList = ref([])
	const userRef = ref()
	const parentCustomerRef = ref()

	// 定义emit事件
	//PROCUREMENT_PARTNERS
	const emit = defineEmits({ successful: null })
	// 默认是关闭状态
	const visible = ref(false)
	const formRef = ref()
	const formData = ref({})

	const submitLoading = ref(false)

	const onHeadChange = (id, item) => {
		formData.value.head = item.name
	}

	// 打开抽屉
	const onOpen = (record) => {
		formData.value = {}
		visible.value = true
		areaList.value = getCombinationList()
		if (record) {
			formData.value = { ...record }
			loadDetail(record.id)
		}
		nextTick(() => {
			userRef.value.onPage({})
			parentCustomerRef.value.onPage({})
		})
	}

	const loadDetail = (id) => {
		customerApi
			.detail({
				id: id
			})
			.then((res) => {
				formData.value.detail = res.detailAddr
			})
			.catch(() => {})
	}

	const onCascaderChange = (_value, selectedOptions) => {
		formData.value.address = selectedOptions.map((o) => o.label).join('')
	}

	// 默认要校验的
	const formRules = {
		custName: [required('请输入客户名称')],
		custCode: [required('客户信用代码:')],
		address: [required('请选择省市区')],
		detailAddr: [required('请输入详细地址')],
		industry: [required('请选择行业')],
		companyPhone: [required('请输入公司电话')],
		contacts: [required('请输入联系人')],
		contactsPhone: [required('请输入联系电话'), rules.phone],
		custBackground: [required('客户背景')],
		headId: [required('请输入负责人')],
		invoiceHeader: [required('请输入发票抬头')],
		dutyPara: [required('请输入税号')],
		bank: [required('请输入开户行')],
		bankAccount: [required('请输入银行账户')]
	}
	// 关闭抽屉
	const onClose = () => {
		visible.value = false
	}

	// 验证并提交数据
	const onSubmit = () => {
		formRef.value
			.validate()
			.then(() => {
				submitLoading.value = true
				let params = { ...formData.value }
				if (params.address instanceof Array) {
					params.address = params.address.join('')
				}
				customerApi
					.addOrEdit(formData.value.id ? 'edit' : 'add', params)
					.then(() => {
						visible.value = false
						emit('successful')
					})
					.finally(() => {
						submitLoading.value = false
					})
			})
			.catch(() => {})
	}
	// 调用这个函数将子组件的一些数据和方法暴露出去
	defineExpose({
		onOpen
	})
</script>

<style scoped :type="less">
	.childAddButton {
		margin-bottom: 10px;
	}
</style>
